<?php $this->_extends('_layouts/main_layout'); ?>
<?php $this->_block('title'); ?>
	Add User
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>
<div id="content" class="box">
	<h1>Add User</h1>
		<?php if (isset($infos)) 
				{
					foreach ($infos as $info)
					{
		?>
				<p class="msg info"><?php echo $info;?></p>
		<?php		}
				}
		?>
		<fieldset>
			<legend>Add User</legend>
		<?php if (isset($errors)) 
				{
					foreach ($errors as $error)
					{
		?>
				<p class="msg error"><?php echo $error;?></p>
		<?php		}
				}
		?>
		<form method="POST" action="<?php echo url('user/add');?>">
		<table class="nostyle">
			<tr>
				<td>Email:</td>
				<td><input type="text" class="input-text" name="email" value="<?php if (isset($email)) echo $email;?>"/></td>
			</tr>
			<tr>
				<td>Name:</td>
				<td><input type="text" class="input-text" name="name" value="<?php if (isset($name)) echo $name;?>"/></td>
			</tr>
			<tr>
				<td>Company:</td>
				<td><input type="text" class="input-text" name="company" value="<?php if (isset($company)) echo $company;?>"/></td>
			</tr>
			<tr>
				<td>Phone:</td>
				<td><input type="text" class="input-text" name="phone" value="<?php if (isset($phone)) echo $phone;?>"/></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" class="input-text" name="password" value=""/></td>
			</tr>
			<tr>
				<td>Confirm Password:</td>
				<td><input type="password" class="input-text" name="password2" value=""/></td>
			</tr>
			<tr id="tr_role">
				<td>Role:</td>
				<td>
					<select name="role" id="role">
						<option value="">--Choose a Role--</option>
						<option value="GAMECO" <?php echo ('GAMECO' == $role) ?  'selected="selected"' : ''; ?>>GAMECO</option>
						<option value="MARKETING" <?php echo ('MARKETING' == $role) ?  'selected="selected"' : ''; ?>>MARKETING</option>
						<option value="PUBLISHER" <?php echo ('PUBLISHER' == $role) ?  'selected="selected"' : ''; ?>>PUBLISHER</option>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" class="t-right"><input type="submit" class="input-submit" value="Save"/></td>
			</tr>
		</table>
		</form>
	</fieldset>
</div>
<script type="text/javascript">
	$(function(){
		$('#role').change(function(){
			if($('#role :selected').val() == 'PUBLISHER'){
				$.get('<?php echo url('user/loadpubs'); ?>', '', function(data){
					var publishers = eval('(' + data + ')')
					var select = '<tr id="tr_publisher">	<td>Publisher:</td>	<td> <select name="publisher" id="publisher">';
					for(var i=0; i<publishers.length; i++){
						select += '<option value="'+publishers[i]['id']+'">'+publishers[i]['name']+'</option>';
					}
					select += '</select></td></tr>';
					$('#tr_role').after(select);
					$.get('<?php echo url('user/loadsubs'); ?>', {id: "1"}, function(data){
						var subs = eval('(' + data + ')')
						var sub = '<tr id="tr_sub">	<td>Sub:</td>	<td> <select name="sub" id="sub">';
						for(var i=0; i<subs.length; i++){
							sub += '<option value="'+subs[i]['id']+'">'+subs[i]['name']+'</option>';
						}
						sub += '</select></td></tr>';
						$('#tr_publisher').after(sub);
					});
				});
			}else{
				$('#tr_publisher').remove();
				$('#tr_sub').remove();
			}
		});
		$('#publisher').live('change',function(){
			var pid = $('#publisher').val();
			$.get('<?php echo url('user/loadsubs'); ?>', {id: pid}, function(data){
				$('#tr_sub').remove();
				var subs = eval('(' + data + ')')
				var sub = '<tr id="tr_sub">	<td>Sub:</td>	<td> <select name="sub" id="sub">';
				for(var i=0; i<subs.length; i++){
					sub += '<option value="'+subs[i]['id']+'">'+subs[i]['name']+'</option>';
				}
				sub += '</select></td></tr>';
				$('#tr_publisher').after(sub);
			});
		});
	});
</script>
<?php $this->_endblock(); ?>
